
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk
import dbus
from _dbus_bindings import BUS_DAEMON_IFACE, BUS_DAEMON_NAME, BUS_DAEMON_PATH
import os
class MessageBox:
  def __init__(self,message_title,message_content):
    self.__load_css()
    self.__load_glade("messagebox.glade")
    self.__extract_widget() 
    self.set_content(message_content)
    self.set_title(message_title)
  def set_title(self,title):
    self.title_label.set_text(title)
  def set_content(self,content):
    self.content_label.set_text(content)
    pass
  def __load_glade(self,glade_file):  
    self.builder = Gtk.Builder()
    self.builder.add_from_file(glade_file)
    self.builder.connect_signals(self)    
  def __extract_widget(self):
    self.window = self.builder.get_object("TopWindow")
    self.title_label = self.builder.get_object("TitleLabel")
    self.content_label = self.builder.get_object("MessageLabel")
  def show(self):
    self.window.show()
  def __load_css(self):
    screen = Gdk.Screen.get_default()
    provider = Gtk.CssProvider()
    provider.load_from_path("messagebox.css")
    Gtk.StyleContext.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
  def ok_btn_click(self, *args):
     self.window.close()
class Cleaner:
  def __init__(self):
    timeout = 60
    flags = 0
    bus_name = "org.dbus.ucleaner"
    opath = "/org/ucleaner"
    #get the system bus
    bus = dbus.SystemBus()
    bus.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE,'StartServiceByName', 'su', (bus_name, flags), timeout=timeout)
    #get the object
    dbus_object = bus.get_object(bus_name, opath)
    #get the interface
    self.dbus_interface = dbus.Interface(dbus_object, "org.ucleaner")

  def clean_apt_cache(self):
    self.dbus_interface.clean_apt_cache()           
  def clean_logs(self):
    self.dbus_interface.clean_logs()            
  def remove_unused_pkgs(self):
    self.dbus_interface.auto_remove_pkgs()
  def remove_thumbnails(self):
    os.system("rm -rf ~/.cache/thumbnails/*")             
  def clean_firefox_cache(self):
    os.system("rm ~/.mozilla/firefox/*.default*/*.sqlite ~/.mozilla/firefox/*default*/sessionstore.js")
    os.system("rm -r ~/.cache/mozilla/firefox/*.default*/*")

class GUI:
  IsWindowMaximized=False
  def __init__(self):
    self.cleaner=Cleaner()
    self.__load_glade("cleaner.glade")
    self.__load_css()    
    self.__extract_widget()
  def __load_glade(self,glade_file):
    self.builder = Gtk.Builder()
    self.builder.add_from_file(glade_file)
    self.builder.connect_signals(self)
  def __extract_widget(self):
    self.window = self.builder.get_object("TopWindow")
  def show(self):
    self.window.show()
  def __load_css(self):
    screen = Gdk.Screen.get_default()
    provider = Gtk.CssProvider()
    provider.load_from_path("cleaner.css")
    Gtk.StyleContext.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
  def maximize(self, button):
    if IsWindowMaximized:
        Gtk.Window.normlize(self.window)
        IsWindowMaximized=False
        return
    Gtk.Window.maximize(self.window)
  def minimize(self, button):
    Gtk.Window.minimize(self.window)
  def close(self, *args):
    Gtk.main_quit()
    exit(0)
  def Clean(self, button):
    status=False
    if self.builder.get_object("AptCacheChk").get_active():
        status=True
        self.cleaner.clean_apt_cache()
    if self.builder.get_object("LogsChk").get_active():
        status=True
        self.cleaner.clean_logs()
    if self.builder.get_object("UnUsedPkgsChk").get_active():
        status=True
        self.cleaner.remove_unused_pkgs()
    if self.builder.get_object("ThumbnailsChk").get_active():
        status=True
        self.cleaner.remove_thumbnails()
    if self.builder.get_object("FireFoxChk").get_active():
        status=True
        self.cleaner.clean_firefox_cache()
    if status:
        MessageBox("Result","Cleaning Completed").show()
        return
    MessageBox("Error","Select At Least one Task").show()
if __name__ == "__main__":
  main = GUI()
  main.show()
  Gtk.main()